package org.gwtlive.pointwork.tasklog.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventPreview;

/**
 * User: fm-sun
 * Date: 2006/07/24
 * Time: 12:04:42
 */
public class Tasklog implements EntryPoint {




    public void onModuleLoad() {


        AbsolutePanel container = new AbsolutePanel();
        //container.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);

        container.addStyleName("TimeRange");
        container.setPixelSize(800, 50);

        FocusPanel mover = new FocusPanel();
        mover.setPixelSize(90, 50);
        mover.addStyleName("Mover");
        //Image resizer = new Image("resizer.gif");
        FocusPanel resizer = new FocusPanel();
        resizer.setPixelSize(10, 50);
        resizer.addStyleName("Resizer");

        container.add(mover, 0, 0);
        container.add(resizer, 90, 0);

        mover.addMouseListener(new MouseListenerAdapter() {
            public void onMouseDown(Widget source, int x, int y) {
                DOM.setCapture(source.getElement());
                dragStartX = x;
                dragStartY = y;
                inDrag = true;
                GWT.log("FocusPanel onMouseDown x = " + x + ", y = " + y, null);
            }

            public void onMouseMove(Widget source, int x, int y) {
                if (inDrag) {
                    int xAbs = x + source.getParent().getAbsoluteLeft();
                    int parentTop = source.getParent().getAbsoluteTop();
                    ((AbsolutePanel)source.getParent()).setWidgetPosition(source.getParent(), xAbs - dragStartX, parentTop);
                    GWT.log("FocusPanel onMouseMove x = " + x + ", y = " + y, null);
                }
            }

            public void onMouseUp(Widget source, int x, int y) {
                DOM.releaseCapture(source.getElement());
                inDrag = false;
                GWT.log("FocusPanel onMouseUp x = " + x + ", y = " + y, null);
            }

            private boolean inDrag;
            private int dragStartX;
            private int dragStartY;
        });

        resizer.addMouseListener(new MouseListenerAdapter(){

            public void onMouseDown(Widget source, int x, int y) {
                DOM.setCapture(source.getElement());
                dragStartX = x;
                dragStartY = y;
                offsetWidth = source.getOffsetWidth();
                inDrag = true;
                GWT.log("resizer onMouseDown x = " + x + ", y = " + y, null);
            }

            public void onMouseMove(Widget source, int x, int y) {
                if (inDrag) {
                    //source.getParent().setWidth(x - dragStartX + offsetWidth + "px");
                    int xAbs = x + source.getAbsoluteLeft();
                    //int parentTop = source.getAbsoluteTop();
                    ((AbsolutePanel)source.getParent()).setWidgetPosition(source, xAbs - dragStartX, 0);
                    GWT.log("resizer onMouseMove x = " + x + ", y = " + y, null);
                }
            }

            public void onMouseUp(Widget source, int x, int y) {
                DOM.releaseCapture(source.getElement());
                inDrag = false;
                GWT.log("resizer onMouseUp x = " + x + ", y = " + y, null);
            }
            private int dragStartX;
            private int dragStartY;
            private boolean inDrag;
            private int offsetWidth;
        });
        DOM.addEventPreview(new EventPreview() {
            public boolean onEventPreview(Event event) {
                switch (DOM.eventGetType(event)) {
                    case Event.ONMOUSEDOWN:
                    case Event.ONMOUSEMOVE:
                    case Event.ONMOUSEUP:
                        DOM.eventPreventDefault(event);
                }
                return true;
            }
        });


        RootPanel.get().add(container);
    }
//    public void onModuleLoad() {
//        final HorizontalPanel panel = new HorizontalPanel();
//        panel.addStyleName("timeRange");
//        final Image resizer = new Image("resize.gif");
////        resizer.addStyleName("Resizer");
//        panel.add(resizer);
//        panel.setCellHorizontalAlignment(resizer, HorizontalPanel.ALIGN_RIGHT);
//        panel.setPixelSize(100,50);
//        resizer.setPixelSize(10,50);
//
//
//        resizer.addMouseListener(new MouseListenerAdapter() {
//
//            public void onMouseDown(Widget widget, int x, int y) {
//                DOM.setCapture(widget.getElement());
//                xOffsetOfPanel = x;
//                yOffset_ = y;
//                inDragOfPanel = true;
//                offsetWidthOfPanel = panel.getOffsetWidth();
//
//            }
//
//            public void onMouseMove(Widget widget, int x, int y) {
//                if (inDragOfPanel) {
//                    //panel.setWidth(x - xOffsetOfPanel + offsetWidthOfPanel + "px");
//                    ((AbsolutePanel) panel.getParent()).setWidgetPosition(panel, x - xOffsetOfPanel, y - yOffset_);
//                    //offsetHeight_ = widget.getOffsetHeight();
////        int resizerLeft = panel.getAbsoluteLeft() + panel.getOffsetWidth() - resizer.getOffsetWidth()-50;
////        GWT.log("panel.getAbsoluteLeft() = " + panel.getAbsoluteLeft(), null);
////        GWT.log("panel.getOffsetWidth() = " + panel.getOffsetWidth(), null);
////        GWT.log("resizer.getOffsetWidth() = " + resizer.getOffsetWidth(), null);
////        GWT.log("resizer.getAbsoluteLeft() = " + resizer.getAbsoluteLeft() , null);
////        panel.setWidgetPosition(resizer, resizerLeft
////                ,resizer.getAbsoluteTop());
//                }
//            }
//
//            public void onMouseUp(Widget widget, int x, int y) {
//                DOM.releaseCapture(widget.getElement());
//                inDragOfPanel = false;
//            }
//        });
//        DOM.addEventPreview(new EventPreview() {
//            public boolean onEventPreview(Event event) {
//                switch (DOM.eventGetType(event)) {
//                    case Event.ONMOUSEDOWN:
//                    case Event.ONMOUSEMOVE:
//                    case Event.ONMOUSEUP:
//                        DOM.eventPreventDefault(event);
//                }
//                return true;
//            }
//        });
//        RootPanel.get().add(panel);
//    }
}
